package edu.hawaii.examschd.commands;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar.*;
import java.util.Calendar;
import java.util.Locale;
import edu.hawaii.examschd.modules.ExamScheduler;

/**
 * Defines the get-exam command.
 * 
 */
public class GetExam implements Command {
  private String name, description, usage, input;
  private ExamScheduler client;

  /**
   * Creates an instance of this command with the client set to null by default, requiring
   * initialization.
   */
  public GetExam() {
    this.name = "get-exam";
    this.usage = "get-exam [crn]";
    this.description = "Gets the exam time for a class with the specified CRN.";
  }

  /**
   * Initializes this command with a healthy ExamScheduler client.
   * 
   * @param client A healthy client used to initialize the command
   * @param mod Module instance that is used for this command.
   * @param args Arguments required by the command for execution
   * @return Whether or not the arguments passed was accepted by the command initialization
   */
  @Override
  public boolean initializeCommand(ExamScheduler client, String[] args) {
    this.client = client;
    if (args.length != 2) {
      System.out.format("The arguments are invalid " + "(Enter \"help\" to see command usage");
      return false;
    }
    this.input = args[1];
    return true;
  }

  /**
   * Returns the name of this command.
   * 
   * @return the name of this command
   */
  @Override
  public String getName() {
    return this.name;
  }

  /**
   * Returns the usage syntax of this command.
   * 
   * @return the usage syntax of this command
   */
  @Override
  public String getUsage() {
    return this.usage;
  }

  /**
   * Returns the description of this command.
   * 
   * @return a description of this command
   */
  @Override
  public String getDescription() {
    return this.description;
  }

  /**
   * Initialize the SourceData with the power data obtained from the client.
   * 
   * @param name The name of the tower or lounge to get data from
   * @return Whether or not the data was successfully initialized
   */
  public boolean initializeData(String name) {
    return true;
  }

  /**
   * Prints the results of executing this command.
   */
  @Override
  public void printResults() {
    int temp = Integer.parseInt(this.input);
    Calendar cal = this.client.get_by_crn_as_cal(temp);
    if (cal == null) {
      System.out.println("ERROR: unable to find record for " + temp);
      return;
    }
    String result = cal_to_human(cal);
    System.out.println(result);

  }

  /**
   * Format calendar string response to be human readable;
   * 
   * @param cal Input calendar
   * @return output Structured readable string
   */
  private String cal_to_human(Calendar cal) {
    Locale locale = Locale.getDefault();
    int style = cal.LONG;
    String output = "";
    DateFormat df = new SimpleDateFormat("EEEE MMMMM dd, yyyy  hh:mm aaa z");
    if (cal == null) {
      return null;
    }
    System.out.println(df.format((cal.getTime())));

    return output;
  }

  // public static void main(String[] args) {
  // CurrentPower cp = new CurrentPower();
  // String[] arg = { "current-power", "Ilima-A" };
  // WattDepotClient cl = new WattDepotClient(args[0]);
  // if (cp.initializeCommand(cl, arg)) {
  // if (cp.initializeData(arg[1])) {
  // cp.printResults();
  // }
  // }

  // }
}